package com.luojing.designModel.zhuangshiqi.t1;

/**
 * @author luojing
 * @create 2018/09/15 上午10:11
 **/
public class Basic {

  private String value;

  public String get() {
    return value;
  }

  public void set(String value) {
    this.value = value;
  }
}

class Decorator extends Basic{
  protected Basic basic;

  public Decorator(Basic basic) {
    this.basic = basic;
  }

  @Override
  public String get() {
    return basic.get();
  }

  @Override
  public void set(String value) {
    basic.set(value);
  }
}

class TimeStamped extends Decorator{
  private final long timeStamp;

  public TimeStamped(Basic basic) {
    super(basic);
    this.timeStamp = System.currentTimeMillis();
  }

  public long getStamp(){
    return this.timeStamp;
  }
}

class SerialNumbered extends Decorator{

  private static long counter = 1;

  private final long serialNumber = counter++;

  public SerialNumbered(Basic basic) {
    super(basic);
  }

  public long getSerialNumber(){
    return serialNumber;
  }
}

//装饰器效果只能适用于1层,主要看最后一层
class Decoration{

  public static void main(String[] args) {
    TimeStamped t = new TimeStamped(new Basic());
    TimeStamped t2 = new TimeStamped(new SerialNumbered((new Basic())));

    SerialNumbered s = new SerialNumbered(new Basic());
    SerialNumbered s2 = new SerialNumbered(new TimeStamped(new Basic()));

    System.out.println(t2.getStamp());
    System.out.println(s2.getSerialNumber());
  }

}